package com.hjj.concurrence.syncMethod.t5;

public class MyObject {
    // 只有共享资源的读写访问才需要同步化，如果不是共享资源，那么久没有同步的必要
    public synchronized void methodA() {
        try {
            System.out.println("begin methodA threadName=" + Thread.currentThread().getName());
            Thread.sleep(5000);
            System.out.println("end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void methodB() {
        try {
            System.out.println("begin methodB threadName=" + Thread.currentThread().getName() + " begin time=" + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyObject object = new MyObject();
        new ThreadA(object).start();
        new ThreadB(object).start();
    }
}
